package com.starsea.book.ui.newpage

import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.lxj.xpopup.XPopup
import com.starsea.book.base.VMBaseActivity
import com.starsea.book.databinding.ActivityCategoryDetailsBinding
import com.starsea.book.databinding.ItemBookCommentChildHeaderBinding
import com.starsea.book.ui.book.info.BookInfoViewModel
import com.starsea.book.ui.newpage.adapter.CommentAllAdapter
import com.starsea.book.ui.newpage.pop.CommentPop
import com.starsea.book.ui.newpage.utils.loadRoundImage
import com.starsea.book.utils.toastOnUi
import com.starsea.book.utils.viewbindingdelegate.viewBinding

/**
 * @author Lee
 * @createTime 2024-01-11 20 GMT+8
 * @desc :
 */
class CommentListChildActivity :
    VMBaseActivity<ActivityCategoryDetailsBinding, BookInfoViewModel>() {

    override val binding by viewBinding(ActivityCategoryDetailsBinding::inflate)
    override val viewModel by viewModels<BookInfoViewModel>()

    lateinit var mAdapter: CommentAllAdapter

    var bookId: String = ""
    var commentType: Int = 1
    var parentId: String = ""
    var custName: String? = ""
    var headUrl: String? = ""
    var scoreNumber: Float = 0f
    var commentDesc: String? = ""
    var createTime: String? = ""
    var pageNo: Int = 1
    var headerBinding: ItemBookCommentChildHeaderBinding? = null
    var mPosition = -1

    override fun onActivityCreated(savedInstanceState: Bundle?) {

        bookId = intent.getStringExtra("bookId") ?: ""
        parentId = intent.getStringExtra("parentId") ?: ""
        custName = intent.getStringExtra("custName") ?: ""
        headUrl = intent.getStringExtra("headUrl") ?: ""
        scoreNumber = intent.getFloatExtra("scoreNumber", 0f)
        commentDesc = intent.getStringExtra("commentDesc") ?: ""
        createTime = intent.getStringExtra("createTime") ?: ""

        mAdapter = CommentAllAdapter(this, 1) { value, type, position ->
//            mPosition = position
//            LogUtils.e(">>> ", "mPosition = " + mPosition)
//            reply("2", value.custName ?: "", value.commentId ?: "", position)
            if (type == 3) {
                viewModel.priseComment(
                    value.commentId ?: "",
                    "1",
                    if (value.likeFlag == 1) 2 else 1,
                    position
                )

            }
        }

        viewModel.commentPriseResult.observe(this) {
            if (it.status == 200) {
                if (it.data != -1) {
                    if (it.likeFlag == 2) {
                        mAdapter.getItem(it.data ?: 0)!!.likeFlag = 0
                        mAdapter.getItem(it.data ?: 0)!!.likeCoount -= 1
                    } else {
                        mAdapter.getItem(it.data ?: 0)!!.likeFlag = 1
                        mAdapter.getItem(it.data ?: 0)!!.likeCoount += 1
                    }

                    mAdapter.notifyItemChanged((it.data ?: 0) + 1)

                } else {

                }

            } else if (it.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this)
            } else {
                binding.srlRefresh.finishLoadMore()
//                mAdapter.addItems(it.data!!)
            }
        }


        binding.rvContent.layoutManager = LinearLayoutManager(this)

        binding.rvContent.adapter = mAdapter

        headerBinding =
            ItemBookCommentChildHeaderBinding.inflate(layoutInflater, binding.rvContent, false)

        mAdapter.addHeaderView {
            headerBinding!!
        }

        headerBinding!!.ivHeader.loadRoundImage(this, headUrl)
        headerBinding!!.tvNickName.text = custName ?: ""
        headerBinding!!.tvCommentTime.text = createTime ?: ""
        headerBinding!!.tvCommentContent.text = commentDesc ?: ""
        headerBinding!!.rbRating.rating = scoreNumber / 2

        headerBinding!!.rtvComment.setOnClickListener {
            mPosition = -1
            reply("2", custName ?: "", parentId, 0)
        }


        binding.srlRefresh.setOnRefreshListener {
            pageNo = 1
            getListData()
        }

        binding.srlRefresh.setOnLoadMoreListener {
            pageNo++
            getListData()
        }
        getListData()

        viewModel.bookCommentResult.observe(this) {
            if (it.status == 200) {
                if (mPosition != -1) {
                    mAdapter.getItem(mPosition)!!.replyCount += 1
                    mAdapter.notifyItemChanged(mPosition + 1)
                } else {

                }
                pageNo = 1
                getListData()
                toastOnUi("评论成功")
//                mAdapter.setItems(it.data!!)
            } else if (it.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this@CommentListChildActivity)
            } else {
                binding.srlRefresh.finishLoadMore()
//                mAdapter.addItems(it.data!!)
            }
        }


        viewModel.commentListResult.observe(this) {
            if (it.status == 200) {
                if (pageNo == 1) {
                    binding.srlRefresh.finishRefresh()
                    if (pageNo == 1) {
                        mAdapter.setItems(it.data!!)
                    } else {
                        mAdapter.addItems(it.data!!)
                    }
                    binding.srlRefresh.finishLoadMore()
                    if (it.data!!.size < 10) {
                        binding.srlRefresh.finishLoadMoreWithNoMoreData()
                    }
                }
            } else if (it.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this@CommentListChildActivity)
            } else {
                toastOnUi(it.msg)
            }
        }
    }

    private fun getListData() {
        viewModel.getCommentList(bookId, commentType.toString(), pageNo, 10, parentId)
    }


    fun reply(commentType: String, nickName: String, commentId: String, position: Int) {
        XPopup.Builder(this)
            .isDestroyOnDismiss(true)
            .asCustom(CommentPop(this, nickName, commentType) { content, score ->
                viewModel.replyComment(bookId, commentType, content, score, commentId)
            })
            .show()
    }

}